<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\CategoryModel;
use app\admin\model\CategoryTypeModel;
use tree\Tree;
use app\admin\service\CategoryService;
use app\admin\controller\WxsiteController;

/**
 * Class UserController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   => '标签管理',
 *     'action' => 'default',
 *     'parent' => 'admin/Service/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   => '',
 *     'remark' => '标签管理'
 * )
 */
class TypeimgController extends AdminBaseController
{
    
    /**
     * 标签列表
     * @adminMenu(
     *     'name'   => '标签列表',
     *     'parent' => 'admin/Category/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
       
     	
    	$list=db('category')->alias('c')->field('c.id as site_type_id,c.img,c.name as type_name')->order('id','desc')->where(array('c.type_id'=>5))->paginate(10);
    	$ib_list = db('ib_stype_img')->where(array('incubator_id'=>1))->field('id,incubator_id,img_url,site_type_id')->select();
    	//var_dump($list);
    	// var_dump($ib_list);
    	// $ib_list[0]=array();
    	// var_dump($ib_list);

    	foreach ($list as $key => $value) {
    		if(!empty($ib_list[0])){
    			
    			foreach ($ib_list as $k => $v) {
    				if($v['site_type_id'] == $value['site_type_id']){
    					$list[$key] = array_merge($list[$key],$ib_list[$k]);
    				 break;
    				}else{
    					$arr=array('id'=>0);
    					$list[$key] = array_merge($list[$key],$arr);
    				}
    			}
    		}else{
    		
    			$arr=array('id'=>0);
    			$list[$key] = array_merge($list[$key],$arr);
    		}
    		
    	}
    	// var_dump(json_decode(json_encode($list),true));
  // exit;
    	$page=$list->render();
    	$this->assign('incubator_id',1);
    	$this->assign('list',$list);
    	$this->assign("page", $page);

    	return $this->fetch();
       // var_dump($list);
    }
    

  
    /**
     * 标签修改
     * @adminMenu(
     *     'name'   => '标签修改',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签修改',
     *     'param'  => ''
     * )
     */
    public function edit_site_type()
    {

        if(request()->isPost()){
            $request = $this->request->param();
         
           //var_dump($request);
            $category['site_type_id'] = $request['site_type_id'];
            $category['img_url'] = $request['img_url'];
            $category['incubator_id'] =session('incubator_id');
           if(!empty($request['id'])){
           	 $re = db('ib_stype_img')->where('id',$request['id'])->update($category);
           }else{
           	 $re = db('ib_stype_img')->insert($category);
           }
           
            if($re){
                $this->success('修改成功','index');
            }
            else{
                $this->error('修改失败');
            }
        }
        else{
        	$request = $this->request->param();
        	if(!empty($request['id'])){
        		$type_info = db('ib_stype_img')->where('id',$request['id'])->find();

        	}else{
        		$request['id']='';
        		$type_info = db('category')->where(array('type_id'=>5,'id'=>$request['cate_id']))->field('id as site_type_id, img as img_url')->find();
        	}
        	
            //$data = CategoryModel::get(['id'=>$id,'type_id'=>$type_id]);
            $this->assign('data',$type_info);
          
            $this->assign('id',$request['id']);
            return $this->fetch();
        }
    }
    
    /**
     * 标签状态修改
     * @adminMenu(
     *     'name'   => '标签状态修改',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签状态修改',
     *     'param'  => ''
     * )
     */
    public function status(){
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this->error('参数错误');
        }
        $type = $this->request->param('type');
        $data[$type] = $this->request->param('value',0,'intval');
        $re = CategoryModel::update($data,['cid'=>$id],'status,sort');
        if($re){
            $this->success('修改成功'.$type);
        }
        else{
            $this->error('修改失败');
        }
    }
    
    /**
     * 标签删除
     * @adminMenu(
     *     'name'   => '标签删除',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签删除',
     *     'param'  => ''
     * )
     */
    public function delete(){
        $ids = input('ids/a');
        if(!$ids){
            $this->error("请选择数据!");
        }
        $re = CategoryModel::destroy($ids);
        if($re){
            $this->success('删除成功!');
        }
        else{
            $this->error("删除失败!");
        }
        
    }
    
    /**
     * 标签关联删除
     * @adminMenu(
     *     'name'   => '标签关联删除',
     *     'parent' => 'admin/Category/index',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10,
     *     'icon'   => '',
     *     'remark' => '标签关联删除',
     *     'param'  => ''
     * )
     */
    public function deletealias(){
        $id = input('id');
        $type_id = input('type_id');
        if(empty($id)||empty($type_id)){
            $this->error("请选择数据!");
        }
        $ids = CategoryService::getFullChilds($id, $type_id);
        $re = CategoryModel::destroy(['type_id'=>$type_id,'id'=>['in',$ids]]);
        if($re){
            $this->success('删除成功!');
        }
        else{
            $this->error("删除失败!");
        }
    
    }
}